<?php
/*
	MyHosting by KuJoe <www.jmd.cc>
	A plugin to allow cPanel based web hosting over MyBB 1.6
*/

define("IN_MYBB", 1);
require_once "./global.php";
global $db, $mybb, $cache;
$lang->load("myhosting");

if ($mybb->settings['myhosting_onoff'] == 0) {
    error($lang->myhosting_offline);
}

if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_groups']))) {
    error_no_permission();
}

function genRdm($length) {
    $characters = 'abcdefghijklmnopqrstuvwxyz';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString. rand(10,99);
}

function sendWHM($whmauth,$apisnd) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $whmheader[0] = "Authorization: WHM $whmauth";
    curl_setopt($ch, CURLOPT_HTTPHEADER,$whmheader);		
    curl_setopt($ch, CURLOPT_URL, $apisnd);
    $whmresult = curl_exec($ch);
    curl_close($ch);
    $rslt = simplexml_load_string($whmresult);
    $retn = ($rslt->result->status);
    $retnerr = ($rslt->result->statusmsg);
    if ($retn == 1) {
        return $retn;
    } else {
        return $retnerr;
    }
}

if (!$mybb->input['act']) {
	$uid = $mybb->user['uid'];
	if ($uid != '') {
	$charchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$uid'");
		if ($db->num_rows($charchk) == 0) {
			add_breadcrumb($lang->myhosting);
			
			$tlds = explode(",",$mybb->settings['myhosting_subdom']);
			function generateSelect($name = '', $options = array()) {
				$tldsel = '<select name="'.$name.'">';
				foreach ($options as $option => $value) {
					$tldsel .= '<option value='.$value.'>'.$value.'</option>';
				}
				$tldsel .= '</select>';
				return $tldsel;
			}
			$tldsel = generateSelect('tld', $tlds);
			$myhpkg1 = $mybb->settings['myhosting_pkg1'];
			$myhpkg2 = $mybb->settings['myhosting_pkg2'];
			$myhpkg3 = $mybb->settings['myhosting_pkg3'];
			$myhpkg1desc = $mybb->settings['myhosting_pkg1desc'];
			$myhpkg2desc = $mybb->settings['myhosting_pkg2desc'];
			$myhpkg3desc = $mybb->settings['myhosting_pkg3desc'];
			if ($mybb->settings['myhosting_pkg1'] != '') {
				$myhpkg1chk = "<input type=\"radio\" value=\"1\" name=\"pkg\">";
			}
			else {
				$myhpkg1chk = "";
			}
			if ($mybb->settings['myhosting_pkg2'] != '') {
				$myhpkg2chk = "<input type=\"radio\" value=\"2\" name=\"pkg\">";
			}
			else {
				$myhpkg2chk = "";
			}
			if ($mybb->settings['myhosting_pkg3'] != '') {
				$myhpkg3chk = "<input type=\"radio\" value=\"3\" name=\"pkg\">";
			}
			else {
				$myhpkg3chk = "";
			}
			$myhtos = $mybb->settings['myhosting_tos'];
			$myhip = get_ip();
			$title = $lang->myhosting_order;
			if ($myhpkg1 != "") {
				$myhpkgs1 = $myhpkg1chk ;
			}
			else {
				$myhpkgs1 = '';
			}
			if ($myhpkg2 != "") {
				$myhpkgs2 = '<div style="float: left; padding: 2px; border: 1px black solid;"><center><u><b>' . $myhpkg2 . '</b></u></center><br />' . $myhpkg2desc . '<br /><center>' . $myhpkg2chk . '</center></div>';
			}
			else {
				$myhpkgs2 = '';
			}
			if ($myhpkg3 != "") {
				$myhpkgs3 = '<div style="float: left; padding: 2px; border: 1px black solid;"><center><u><b>' . $myhpkg3 . '</b></u></center><br />' . $myhpkg3desc . '<br /><center>' . $myhpkg3chk . '</center></div>';
			}
			else {
				$myhpkgs3 = '';
			}
			$sdomainblk = $lang->myhosting_sdomainblk;
			$odomainblk = $lang->myhosting_odomainblk;
			if ($mybb->settings['myhosting_owndomains'] == 1) {
				$domainopts = '<div id="sdomain" style="display:block;"><input type="text" name="sdomain" size="20" />.' . $tldsel . '<br /><a href="javascript:;" onmousedown="document.getElementById(\'odomain\').style.display = \'block\'; document.getElementById(\'sdomain\').style.display = \'none\';">' . $sdomainblk . '</a></div><div id="odomain" style="display:none;"><input type="text" name="odomain" size="20" /><br /><a href="javascript:;" onmousedown="document.getElementById(\'sdomain\').style.display = \'block\'; document.getElementById(\'odomain\').style.display = \'none\';">' . $odomainblk . '</a></div>';
			}
			else {
				$domainopts = '<input type="text" name="sdomain" size="20" />.' . $tldsel .'';
			}
			eval("\$myhosting_order = \"".$templates->get("myhosting_order")."\";");
			output_page($myhosting_order);
		}
		else {
			add_breadcrumb($lang->myhosting);
			$title = $lang->myhosting_show;
			$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$uid'"));
			$myhostingfname = $myhq['fname'];
			$myhostinglname = $myhq['lname'];
			$myhostingstate = $myhq['state'];
			$myhostingcountry = $myhq['country'];
			$myhostingdomain = $myhq['domain'];
			$myhostingusr = $myhq['usr'];
			if ($myhq['pkg'] == 1) {
				$myhostingpkg = $mybb->settings['myhosting_pkg1'];
			}
			elseif ($myhq['pkg'] == 2) {
				$myhostingpkg = $mybb->settings['myhosting_pkg2'];
			}
			elseif ($myhq['pkg'] == 3) {
				$myhostingpkg = $mybb->settings['myhosting_pkg3'];
			}
			if ($myhq['status'] == 0) {
				$myhostingstatus = $lang->myhosting_status0;
				$selfcancel = '<a href="javascript:;" onmousedown="document.getElementById(\'cancelblk\').style.display = \'block\';">' . $lang->myhosting_selfcancelact . '</a>';
			}
			elseif ($myhq['status'] == 1) {
				$myhostingstatus = $lang->myhosting_status1;
				$selfcancel = '<a href="javascript:;" onmousedown="document.getElementById(\'cancelblk\').style.display = \'block\';">' . $lang->myhosting_selfcancelact . '</a>';
			}
			elseif ($myhq['status'] == 2) {
				$myhostingstatus = $lang->myhosting_status2;
				$selfcancel = '<a href="javascript:;" onmousedown="document.getElementById(\'cancelblk\').style.display = \'block\';">' . $lang->myhosting_selfcancelact . '</a>';
			}
			elseif ($myhq['status'] == 3) {
				$myhostingstatus = $lang->myhosting_status3;
				$selfcancel = $lang->myhosting_status3;
			}
			$myhostingtime = my_date($mybb->settings['dateformat'].' '.$mybb->settings['timeformat'],$myhq['time']);
			eval("\$myhosting_client = \"".$templates->get("myhosting_client")."\";");
			output_page($myhosting_client);
		}
	}
	else {
		error_no_permission();
	}
}

if ($mybb->input['act'] == "do_order") {
	$uid = $mybb->user['uid'];
	if ($uid != '') {
	$myhuq = $db->fetch_array($db->simple_select("users","*","uid='$uid'"));
	verify_post_check($mybb->input['my_post_key']);
		$time=$db->escape_string(time());
		$uid = $db->escape_string($mybb->input['uid']);
		$email = $myhuq['email'];
		$fusr = $db->escape_string($mybb->input['fusr']);
		$fname = $db->escape_string($mybb->input['fname']);
		$lname = $db->escape_string($mybb->input['lname']);
		$state = $db->escape_string($mybb->input['state']);
		$country = $db->escape_string($mybb->input['country']);
		$usr = genRdm(6);
		$pkg = $db->escape_string($mybb->input['pkg']);
		$regip = $db->escape_string($mybb->input['regip']);
		$skey = $db->escape_string(rand(10000001, 99999999));
		$status = $db->escape_string($mybb->settings['myhosting_adminact']);
		$tos = $db->escape_string($mybb->input['accepttos']);
		$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$uid'");
		if (!$uid) {
			error($lang->myhosting_nouid);
		}
		if (!$fname) {
			error($lang->myhosting_nofname);
		}
		if (!$lname) {
			error($lang->myhosting_nolname);
		}
		if (!$state) {
			error($lang->myhosting_nostate);
		}
		if (!$country) {
			error($lang->myhosting_nocountry);
		}
		if (!$pkg) {
			error($lang->myhosting_nopkg);
		}
		else {
			if ($pkg == 1) {
				$plan = $mybb->settings['myhosting_pkg1bknd'];
			}
			elseif ($pkg == 2) {
				$plan = $mybb->settings['myhosting_pkg2bknd'];
			}
			else {
				$plan = $mybb->settings['myhosting_pkg3bknd'];
			}
		}
		if (!$tos) {
			error($lang->myhosting_notos);
		}
		if ($db->num_rows($usrchk) != 0) {
			error($lang->myhosting_orderlimit);
		}
		if ($mybb->settings['myhosting_owndomains'] == 1) {
			if (!$mybb->input['odomain']) {
				$domain = $db->escape_string($mybb->input['sdomain'] . "." . $mybb->input['tld']);
				if (!$mybb->input['sdomain']) {
					error($lang->myhosting_nodomain);
				}
			} else {
				$domain = $db->escape_string($mybb->input['odomain']);
				if (!$mybb->input['odomain']) {
					error($lang->myhosting_nodomain);
				}
			}
		}
		if ($mybb->settings['myhosting_owndomains'] == 0) {
			$domain = $db->escape_string($mybb->input['sdomain'] . "." . $mybb->input['tld']);
			if (!$mybb->input['sdomain']) {
				error($lang->myhosting_nodomain);
			}
		}

    if ($mybb->settings['myhosting_adminact'] == 1) {
        $passwd = genRdm(10);
        $whmuser = $mybb->settings['myhosting_whmuser'];
        $whmkey = $mybb->settings['myhosting_whmkey'];
        $whmhash = preg_replace("'(\r|\n)'","",$whmkey);
        $whmauth = $whmuser . ":" . $whmhash;
        $apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/createacct?username=" . $usr . "&password=" . $passwd . "&domain=" . $domain . "&plan=" . $plan . "&contactemail=" . $email . "";
        $cmd = sendWHM($whmauth,$apisnd);
        if ($cmd != 1) {
				   $db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
    				   error($lang->myhosting_orderfail);
        }
    }
    $db->insert_query("myhosting",array("uid"=>$uid,"fusr"=>$fusr,"fname"=>$fname,"lname"=>$lname,"state"=>$state,"country"=>$country,"domain"=>$domain,"usr"=>$usr,"email"=>$email,"pkg"=>$pkg,"regip"=>$regip,"skey"=>$skey,"status"=>$status,"time"=>$time));
    $homename = $mybb->settings['homename'];
    $subject = "$homename hosting account created!";
	if ($mybb->settings['myhosting_adminact'] == 1) {
		$message = "Welcome to $homename!\r\n \r\nThank you for creating a hosting account with us. At this time your account is active and ready to be used. Here are your account details:\r\n \r\nUsername: $usr\r\nPassword: $passwd\r\nDomain: $domain\r\ncPanel Access: http://". $mybb->settings['myhosting_hostname'] .":2082\r\nNameservers*:\r\n". $mybb->settings['myhosting_ns1'] ."\r\n". $mybb->settings['myhosting_ns2'] ."\r\nView your account here: ". $mybb->settings['bburl'] ."/myhosting.php\r\n\r\n*If you are using your own domain you will need to use these nameservers, otherwise please ignore them.\r\n\r\nAgain, thank you for joining us!\r\n-$homename Staff";
	} else {
		$message = "Welcome to $homename!\r\n \r\nThank you for creating a hosting account with us. At this time your account is waiting for a staff member who will need to review your account and manually activate it. Here are your account details:\r\n \r\nUsername: $usr\r\nPassword*: Not Available\r\nDomain: $domain\r\ncPanel Access: http://". $mybb->settings['myhosting_hostname'] .":2082\r\nNameservers:\r\n". $mybb->settings['myhosting_ns1'] ."\r\n". $mybb->settings['myhosting_ns2'] ."\r\nView your account here: ". $mybb->settings['bburl'] ."/myhosting.php\r\n\r\n*Your password will be automatically created when your account is activated.\r\n\r\nAgain, thank you for joining us!\r\n-$homename Staff";
	}
	my_mail($email, $subject, $message);
	$msg = 'Account created. (' . $shu . ')';
	$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$msg));
	$subject = "New order on $homename!";
	$message = "$fusr ordered a new hosting account.";
	my_mail($mybb->settings['myhosting_adminemail'], $subject, $message);
	redirect("myhosting.php",$lang->myhosting_ordered);
}
	else {
		error_no_permission();
	}
}

if ($mybb->input['act'] == "do_show") {
	$uid = $mybb->user['uid'];
	if (!$uid) {
		error_no_permission();
	}
	if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	else {
		if (!$mybb->input['shu']) {
			error($lang->myhosting_noclient);
		}
		else {
			$shu = $mybb->input['shu'];
			$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$shu'");
			if ($db->num_rows($usrchk) != 0) {
				add_breadcrumb($lang->myhosting);
				$title = $lang->myhosting_show;
				$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$shu'"));
				$myhostingfname = $myhq['fname'];
				$myhostinglname = $myhq['lname'];
				$myhostingstate = $myhq['state'];
				$myhostingcountry = $myhq['country'];
				$myhostingdomain = $myhq['domain'];
				$myhostingusr = $myhq['usr'];
				if ($myhq['pkg'] == 1) {
					$myhostingpkg = $mybb->settings['myhosting_pkg1'];
				}
				elseif ($myhq['pkg'] == 2) {
					$myhostingpkg = $mybb->settings['myhosting_pkg2'];
				}
				elseif ($myhq['pkg'] == 3) {
					$myhostingpkg = $mybb->settings['myhosting_pkg3'];
				}
				if ($myhq['status'] == 0) {
					$myhostingstatus = $lang->myhosting_status0;
				}
				elseif ($myhq['status'] == 1) {
					$myhostingstatus = $lang->myhosting_status1;
				}
				elseif ($myhq['status'] == 2) {
					$myhostingstatus = $lang->myhosting_status2;
				}
				$myhostingregip = $myhq['regip'];
				$myhostingskey = $myhq['skey'];
				$myhostingtime = my_date($mybb->settings['dateformat'].' '.$mybb->settings['timeformat'],$myhq['time']);
				#$myhnq = $db->simple_select("myhostingnotes", "*", "uid = '$uid'", array('order_by' => 'noteid', 'order_dir' => 'desc'));
				#$myhostingnotes = while($row = $db->fetch_array($myhnq)) { . '
				#<p align="center"><table width="100%" border="1">
				#<tr><th>' . $lang->myhosting_time . '</th>
				#<th>' . $lang->myhosting_msg . '</th>
				#<th>' . $lang->myhosting_sid . '</th>
				#</tr>
				#<tr>
				#<td>' . my_date($mybb->settings['dateformat'].' '.$mybb->settings['timeformat'],$row['time']) . '</td>
				#<td>' . $db->escape_string($row['msg']) . '</td>
				#<td>' . $db->escape_string($row['sid']) . '</td>
				#</tr>
				#</table></p>' . ;
				#}
				eval("\$myhosting_show = \"".$templates->get("myhosting_show")."\";");
				output_page($myhosting_show);
			}
			else {
				error($lang->myhosting_noclient);
			}
		}
	}
}

if ($mybb->input['act'] == "do_save") {
	$suid = $mybb->input['suid'];
	$uid = $mybb->input['shu'];
	$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$shu'");
	$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$shu'"));
	verify_post_check($mybb->input['my_post_key']);
	if (!$mybb->input['shu']) {
		error($lang->myhosting_noclient);
	}
	elseif ($uid != $mybb->input['shu'] AND !in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	else {
		$time=$db->escape_string(time());
		$fname = $db->escape_string($mybb->input['fname']);
		$lname = $db->escape_string($mybb->input['lname']);
		$state = $db->escape_string($mybb->input['state']);
		$country = $db->escape_string($mybb->input['country']);
		$domain = $db->escape_string($mybb->input['domain'] . $mybb->input['tld']);
		$usr = $db->escape_string($mybb->input['usr']);
		$skey = $db->escape_string(md5($mybb->input['domain']));
		$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$uid'");
		if (!$uid) {
			error($lang->myhosting_nouid);
		}
		if (!$fname) {
			error($lang->myhosting_nofname);
		}
		if (!$lname) {
			error($lang->myhosting_nolname);
		}
		if (!$state) {
			error($lang->myhosting_nostate);
		}
		if (!$country) {
			error($lang->myhosting_nocountry);
		}
		if (!$domain) {
			error($lang->myhosting_nodomain);
		}
		$db->update_query("myhosting",array("usr"=>$usr,"fname"=>$fname,"lname"=>$lname,"state"=>$state,"country"=>$country,"domain"=>$domain),"uid={$mybb->input['shu']}",1);
		$msg = 'Account updated. (' . $shu . ' by ' . $uid . ')';
		$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$msg));
		redirect("myhosting.php?act=do_show&shu={$mybb->input['shu']}",$lang->myhosting_saved);
	}
}

if ($mybb->input['act'] == "do_activate") {
	$uid = $mybb->user['uid'];
	$shu = $mybb->input['shu'];
	verify_post_check($mybb->input['my_post_key']);
	$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$shu'");
	$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$shu'"));
	if (!$uid) {
		error_no_permission();
	}
	if (!$shu) {
		error_no_permission();
	}
	if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	else {
		$usr = $myhq['usr'];
		$domain = $myhq['domain'];
		if ($myhq['pkg'] == 1) {
			$plan = $mybb->settings['myhosting_pkg1bknd'];
		}
		elseif ($myhq['pkg'] == 2) {
			$plan = $mybb->settings['myhosting_pkg2bknd'];
		}
		elseif ($myhq['pkg'] == 3) {
			$plan = $mybb->settings['myhosting_pkg3bknd'];
		}
		$email = $myhq['email'];
        	$passwd = genRdm(10);
	        $whmuser = $mybb->settings['myhosting_whmuser'];
	        $whmkey = $mybb->settings['myhosting_whmkey'];
        	$whmhash = preg_replace("'(\r|\n)'","",$whmkey);
	        $whmauth = $whmuser . ":" . $whmhash;
        	$apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/createacct?username=" . $myhq['usr'] . "&password=" . $passwd . "&domain=" . $myhq['domain'] . "&plan=" . $plan . "&contactemail=" . $myhq['email'] . "";
	        $cmd = sendWHM($whmauth,$apisnd);
	        if ($cmd != 1) {
			$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$db->escape_string($cmd)));
			error($lang->myhosting_orderfail);
		}
		$db->update_query("myhosting",array("status"=>"1"),"uid={$mybb->input['shu']}",1);
		$homename = $mybb->settings['homename'];
		$subject = "$homename hosting account activated!";
		$message = "Your hosting account is now activated!\r\n\r\nYour password is: $passwd\r\n\r\n-$homename Staff";
		my_mail($email, $subject, $message);
		$msg = 'Account activated. (' . $shu . ' by ' . $uid . ')';
		$db->insert_query("myhostinglogs",array("uid"=>$shu,"time"=>$time,"msg"=>$msg));
			redirect("../" . $mybb->config['admin_dir'] . "/index.php?module=config/myhosting_admin");
	}
}

if ($mybb->input['act'] == "do_suspend") {
	$uid = $mybb->user['uid'];
	$shu = $mybb->input['shu'];
	$reason = $mybb->input['rsn'];
	verify_post_check($mybb->input['my_post_key']);
	$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$shu'");
	$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$shu'"));
	if (!$uid) {
		error_no_permission();
	}
	if (!$shu) {
		error_no_permission();
	}
	if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	else {

	        $whmuser = $mybb->settings['myhosting_whmuser'];
	        $whmkey = $mybb->settings['myhosting_whmkey'];
        	$whmhash = preg_replace("'(\r|\n)'","",$whmkey);
	        $whmauth = $whmuser . ":" . $whmhash;
        	$apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/suspendacct?user=" . $myhq['usr'] . "";
	        $cmd = sendWHM($whmauth,$apisnd);
	        if ($cmd != 1) {
			$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
			error($lang->myhosting_suspendfail);
		}
		$db->update_query("myhosting",array("status"=>"2"),"uid={$mybb->input['shu']}",1);
		$email = $myhq['email'];
		$homename = $mybb->settings['homename'];
		$subject = "$homename hosting account suspended!";
		$message = "Your hosting account has been suspended!\r\n\r\nReason given: $reason\r\n\r\n-$homename Staff";
		my_mail($email, $subject, $message);
		$msg = 'Account suspended. (' . $shu . ' by ' . $uid . ' Reason: ' . $reason . ')';
		$db->insert_query("myhostinglogs",array("uid"=>$shu,"time"=>$time,"msg"=>$msg));
			redirect("../" . $mybb->config['admin_dir'] . "/index.php?module=config/myhosting_admin");
	}
}

if ($mybb->input['act'] == "do_unsuspend") {
	$uid = $mybb->user['uid'];
	$shu = $mybb->input['shu'];
	verify_post_check($mybb->input['my_post_key']);
	$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$shu'");
	$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$shu'"));
	if (!$uid) {
		error_no_permission();
	}
	if (!$shu) {
		error_no_permission();
	}
	if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	else {

	        $whmuser = $mybb->settings['myhosting_whmuser'];
	        $whmkey = $mybb->settings['myhosting_whmkey'];
        	$whmhash = preg_replace("'(\r|\n)'","",$whmkey);
	        $whmauth = $whmuser . ":" . $whmhash;
        	$apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/unsuspendacct?user=" . $myhq['usr'] . "";
	        $cmd = sendWHM($whmauth,$apisnd);
	        if ($cmd != 1) {
			$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
			error($lang->myhosting_unsuspendfail);
		}
		$db->update_query("myhosting",array("status"=>"1"),"uid={$mybb->input['shu']}",1);
		$email = $myhq['email'];
		$homename = $mybb->settings['homename'];
		$subject = "$homename hosting account activated!";
		$message = "Your hosting account has been unsuspended!\r\n\r\n-$homename Staff";
		my_mail($email, $subject, $message);
		$msg = 'Account unsuspended. (' . $shu . ' by ' . $uid . ')';
		$db->insert_query("myhostinglogs",array("uid"=>$shu,"time"=>$time,"msg"=>$msg));
			redirect("../" . $mybb->config['admin_dir'] . "/index.php?module=config/myhosting_admin");
	}
}

if ($mybb->input['act'] == "do_cancel") {
	$uid = $mybb->user['uid'];
	$shu = $mybb->input['shu'];
	$reason = $mybb->input['rsn'];
	verify_post_check($mybb->input['my_post_key']);
	$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$shu'");
	$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$shu'"));
	if (!$uid) {
		error_no_permission();
	}
	if (!$shu) {
		error_no_permission();
	}
	if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	else {

	        $whmuser = $mybb->settings['myhosting_whmuser'];
	        $whmkey = $mybb->settings['myhosting_whmkey'];
        	$whmhash = preg_replace("'(\r|\n)'","",$whmkey);
	        $whmauth = $whmuser . ":" . $whmhash;
        	$apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/removeacct?user=" . $myhq['usr'] . "";
	        $cmd = sendWHM($whmauth,$apisnd);
	        if ($cmd != 1) {
			$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
			error($lang->myhosting_unsuspendfail);
		}
		$db->update_query("myhosting",array("status"=>"3"),"uid={$mybb->input['shu']}",1);
		$email = $myhq['email'];
		$homename = $mybb->settings['homename'];
		$subject = "$homename hosting account cancelled!";
		$message = "Your hosting account has been cancelled!\r\n\r\nReason given: $reason\r\n\r\n-$homename Staff";
		my_mail($email, $subject, $message);
		$msg = 'Account cancelled. (' . $shu . ' by ' . $uid . ' Reason ' . $reason .')';
		$db->insert_query("myhostinglogs",array("uid"=>$shu,"time"=>$time,"msg"=>$msg));
			redirect("../" . $mybb->config['admin_dir'] . "/index.php?module=config/myhosting_admin");
	}
}

if ($mybb->input['act'] == "do_selfcancel") {
	$uid = $mybb->user['uid'];
	$chk = $mybb->input['cancelchk'];
	$reason = $mybb->input['rsn'];
	verify_post_check($mybb->input['my_post_key']);
	$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$uid'");
	$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$uid'"));
	if (!$uid) {
		error_no_permission();
	}
	if ($myhq['status'] == 3) {
		error($lang->myhosting_alreadycancelled);
	}
	if (!$chk) {
		error($lang->myhosting_nocancelchk);
	}
	else {
		if ($myhq['status'] >= 1) {
	        $whmuser = $mybb->settings['myhosting_whmuser'];
	        $whmkey = $mybb->settings['myhosting_whmkey'];
        	$whmhash = preg_replace("'(\r|\n)'","",$whmkey);
	        $whmauth = $whmuser . ":" . $whmhash;
        	$apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/removeacct?user=" . $myhq['usr'] . "";
	        $cmd = sendWHM($whmauth,$apisnd);
	        if ($cmd != 1) {
			$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
			error($lang->myhosting_unsuspendfail);
			}
		}
		$db->update_query("myhosting",array("status"=>"3"),"uid={$mybb->user['uid']}",1);
		$email = $myhq['email'];
		$homename = $mybb->settings['homename'];
		$subject = "$homename hosting account cancelled!";
		$message = "You have cancelled you hosting account!\r\n\r\nReason given: $reason\r\n\r\n-$homename Staff";
		my_mail($email, $subject, $message);
		$msg = 'Account self cancelled. (' . $uid . ' by ' . $uid . ' Reason: ' . $reason . ')';
		$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$msg));
			redirect("../myhosting.php");
	}
}

if ($mybb->input['act'] == "do_delete") {
	$uid = $mybb->user['uid'];
	$shu = $mybb->input['shu'];
	verify_post_check($mybb->input['my_post_key']);
	$usrchk=$db->query("SELECT * FROM ".TABLE_PREFIX."myhosting WHERE uid='$shu'");
	$myhq = $db->fetch_array($db->simple_select("myhosting","*","uid='$shu'"));
	if (!$uid) {
		error_no_permission();
	}
	if (!$shu) {
		error_no_permission();
	}
	if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	if ($myhq['status'] == 3 OR $myhq['status'] == 0) {
		$db->delete_query("myhosting","uid={$mybb->input['shu']}",1);
		$msg = 'Account deleted. (' . $shu . ' by ' . $uid . ')';
		$db->insert_query("myhostinglogs",array("uid"=>$shu,"time"=>$time,"msg"=>$msg));
			redirect("../" . $mybb->config['admin_dir'] . "/index.php?module=config/myhosting_admin");
	}	
	else {
	        $whmuser = $mybb->settings['myhosting_whmuser'];
	        $whmkey = $mybb->settings['myhosting_whmkey'];
        	$whmhash = preg_replace("'(\r|\n)'","",$whmkey);
	        $whmauth = $whmuser . ":" . $whmhash;
        	$apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/removeacct?user=" . $myhq['usr'] . "";
	        $cmd = sendWHM($whmauth,$apisnd);
	        if ($cmd != 1) {
			$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
			error($lang->myhosting_deletefail);
		}
		$db->delete_query("myhosting","uid={$mybb->input['shu']}",1);
		$msg = 'Account deleted. (' . $shu . ' by ' . $uid . ')';
		$db->insert_query("myhostinglogs",array("uid"=>$shu,"time"=>$time,"msg"=>$msg));
			redirect("../" . $mybb->config['admin_dir'] . "/index.php?module=config/myhosting_admin");
	}
}

/* Will work on this more later, I am still an XML newb. :(
if ($mybb->input['act'] == "do_import") {
	$uid = $mybb->user['uid'];
	verify_post_check($mybb->input['my_post_key']);
	if (!$uid) {
		error_no_permission();
	}
	if (!in_array($mybb->user['usergroup'],explode(",",$mybb->settings['myhosting_modgroups']))) {
		error_no_permission();
	}
	else {
	        $whmuser = $mybb->settings['myhosting_whmuser'];
	        $whmkey = $mybb->settings['myhosting_whmkey'];
        	$whmhash = preg_replace("'(\r|\n)'","",$whmkey);
	        $whmauth = $whmuser . ":" . $whmhash;
        	$apisnd = "https://" . $mybb->settings['myhosting_hostname'] . ":2087" . "/xml-api/listaccts?searchtype=owner&search=" . $whmuser . "";
	        $cmd = sendWHM($whmauth,$apisnd);
	        if ($cmd != 1) {
			$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
			error($lang->myhosting_importfail);
		}
		$db->insert_query("myhostinglogs",array("uid"=>$uid,"time"=>$time,"msg"=>$cmd));
			redirect("../" . $mybb->config['admin_dir'] . "/index.php?module=config/myhosting_admin");
	}
}
*/

if ($mybb->input['act'] == "do_debug") {
	die();
}

?>
